/*
 * CameraAerial.cpp
 *
 *  Created on: 23/10/2012
 *      Author: Matias_2
 */

#include "cameras/CameraAerial.h"
#include "GL/glew.h"
#include <iostream> // FIXME

CameraAerial::CameraAerial() {
	velocityRotationAngleX = 0;
	velocityRotationAngleY = 0;
	rotationAngleX = 0;
	rotationAngleY = 0;
	distance = AERIAL_INIT_DISTANCE;
}

CameraAerial::~CameraAerial() { }

glm::mat4 CameraAerial::getViewMatrix() {
	glm::mat4 transformation = glm::mat4 (1.0f);
	transformation = glm::lookAt(glm::vec3(DEFAULT_X, DEFAULT_Y + distance, DEFAULT_Z),
			glm::vec3(DEFAULT_LOOK_AT_X, DEFAULT_LOOK_AT_Y, DEFAULT_LOOK_AT_Z), glm::vec3(0,0,1));
	transformation = glm::translate(transformation, glm::vec3(DEFAULT_LOOK_AT_X,
				DEFAULT_LOOK_AT_Y, DEFAULT_LOOK_AT_Z));
	transformation = glm::rotate(transformation, rotationAngleY, glm::vec3(1.0f, 0.0f, 0.0f));
	transformation = glm::rotate(transformation, -rotationAngleX, glm::vec3(0.0f, 0.0f, 1.0f));
	transformation = glm::translate(transformation, glm::vec3(-DEFAULT_LOOK_AT_X,
			-DEFAULT_LOOK_AT_Y, -DEFAULT_LOOK_AT_Z));
	return transformation;
}

void CameraAerial::OnMouseMove(int x, int y) {
	// get size of viewport
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);

	// reference (x,y) from de center of the viewport
	velocityRotationAngleX = ((viewport[2]/2 -  x) * AERIAL_SPEED_ROTATION);
	rotationAngleX += velocityRotationAngleX;
	if (rotationAngleX > AERIAL_MAX_ROTATION_X)
		rotationAngleX -= AERIAL_MAX_ROTATION_X;
	if (rotationAngleX < -AERIAL_MAX_ROTATION_X)
		rotationAngleX += AERIAL_MAX_ROTATION_X;

	velocityRotationAngleY = ((viewport[3]/2 - y) * AERIAL_SPEED_ROTATION);
	rotationAngleY += velocityRotationAngleY;
	if (rotationAngleY > AERIAL_MAX_ROTATION_Y)
		rotationAngleY = AERIAL_MAX_ROTATION_Y;
	if (rotationAngleY < 0)
		rotationAngleY = 0;
}

void CameraAerial::OnKeyDown(int nKey, char cAscii) {
	if (cAscii == '+') {
		distance += AERIAL_SPEED_ZOOM;
		if (distance > AERIAL_MAX_ZOOM)
			distance = AERIAL_MAX_ZOOM;
	} else if (cAscii == '-') {
		distance -= AERIAL_SPEED_ZOOM;
	}
}

void CameraAerial::registerListener() {
	InputMgr::getInstance()->registerListener((MouseListener*) this);
	InputMgr::getInstance()->registerListener((KeyListener*) this);
};

void CameraAerial::deleteListener() {
	InputMgr::getInstance()->deleteListener((MouseListener*) this);
	InputMgr::getInstance()->deleteListener((KeyListener*) this);
};



